const app = getApp();
const {upTime} = require('/util/public');
Page({
    data: {
        query:'',
        productOrderRecordVoList:[],

        buyBreakSta: false, //是否是归还订单

        surplusNum: 0,
        thisMonthIndex: 0,
        oid: '',
        goodsData: {}
    },
    onLoad(query) {
        let id = query.param;
        this.setData({
          query,
          oid: id
        });
        this.getoOrderBill(id);
    },

    navigateTo(e){
      app.navigateTo(e);
    },
    navigateToT(e){
      if(this.data.surplusNum < 1){
        my.showToast({
          content: '该账单已经全部还清！'
        })
        return;
      }
      app.navigateTo(e);
    },

    // 获取账单数据
    getoOrderBill(id){
        const time = new Date().setHours(0,0,0,0)/1000+(24*60*60);
        console.log(888999,(time));

        
        let timer = new Date();
        let yy = timer.getFullYear();
        let MM = timer.getMonth() + 1;
        let date = timer.getDate() ;
        MM = MM < 10 ? ('0' + MM) : MM;
        date = date < 10 ? ('0' + date) : date;

        let nowTimer = yy+'/'+MM+'/'+date;


        app.order.detail(id)
        .then(res=>{
            console.log(res)
            let goodsData =  JSON.parse(res.data.params);
            let goodsType = goodsData.detail.split(' ')[0];
            console.log(999,goodsType)
            if(goodsType == '租完归还'){
              this.setData({
                buyBreakSta: true
              })
            }

            let productOrderRecordVoList = res.data.productOrderRecordVoList;

            let thisMonthIndex = 0;

            productOrderRecordVoList.forEach((item,index)=>{
                item.thisMonth = false;
                // item.showBtn = false;
                
                // if(time>item.expiretime) {
                //     item.showBtn = true
                // };
                
                item.time = upTime("%Y/%M/%D",time);
                item.createtime = upTime("%Y/%M/%D",item.createtime);
                item.duetime = upTime("%Y/%M/%D",item.duetime);
                item.expiretime = upTime("%Y/%M/%D",item.expiretime);
                item.price = (item.price/100).toFixed(2);

                // let extimTimer = item.expiretime.substring(0,7);
                let extimTimer = item.expiretime;

                console.log('账单日期',extimTimer)
                console.log('今日日期',nowTimer)
                if(extimTimer == nowTimer){
                  item.thisMonth = true;
                  thisMonthIndex = index;
                }

                
            })

            let timehan = productOrderRecordVoList[thisMonthIndex].expiretime.split('/');
            productOrderRecordVoList[thisMonthIndex].timerHanZi = timehan[0] + '年' + timehan[1] + '月' + timehan[2] + '日'

            let PriceTrue = 0;
            let PriceFalse = productOrderRecordVoList.length;
            productOrderRecordVoList.forEach(item=>{
              if(item.payment==1){
                PriceTrue+=1;
              }
            })

            let surplusNum = PriceFalse - PriceTrue;
            

            this.setData({
                productOrderRecordVoList,
                surplusNum,
                thisMonthIndex,
                goodsData
            })
        })
    },



    // 支付账单
    async onpay(e){
      console.log(e)
        let paymentSta = e.target.dataset.param.payment;
        if(paymentSta){
          my.showToast({
            content:'本期已经还款过了哦~'
          })
          return;
        }


        const oid = e.target.dataset.param.oid,
        id = e.target.dataset.param.id,
        index = this.data.thisMonthIndex;
        const res = await app.order.alipay(oid,id);
        // console.log(res);
        const pay = await app.alipay(res.data);
        // console.log(pay);
        if(pay.resultCode == 9000) {
            let productOrderRecordVoList = this.data.productOrderRecordVoList;
            productOrderRecordVoList[index].payment = 1;
            this.setData({productOrderRecordVoList});
        };
    },

    
    // 下拉刷新
    onPullDownRefresh() {
        this.onLoad(this.data.query);
        setTimeout(() => {
            my.stopPullDownRefresh({});
        }, 1000)
    },
    stopPullDownRefresh() {
        my.stopPullDownRefresh({});
    }
});
